<!doctype html>
<html>
<head>
    <meta charset="utf-8">
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1">
	<link rel="__ROOT__/Public/Admin/shortcut icon" href="favicon.ico">
	<link href="__ROOT__/Public/Admin/css/bootstrap.min.css?v=3.3.6" rel="stylesheet">
	<link href="__ROOT__/Public/Admin/css/animate.css" rel="stylesheet">
	<link href="__ROOT__/Public/Admin/css/style.css?v=4.1.0" rel="stylesheet">
	
	<link href="__ROOT__/Public/Admin/js/plugins/bootstrap-editable/css/bootstrap-editable.css" rel="stylesheet" />
	<link href="__ROOT__/Public/Admin/js/plugins/bootstrap221/css/editable.css" rel="stylesheet" />
	<link href="__ROOT__/Public/Admin/js/plugins/sweetalert/sweetalert.css" rel="stylesheet">
	<style>
.table > thead > tr > th, .table > tbody > tr > th, .table > tfoot > tr > th, .table > thead > tr > td, .table > tbody > tr > td, .table > tfoot > tr > td {
	height:35px;
}
.input-medium{
	border-radius: 0px !important;
}

	</style>
	
</head>
<body class="gray-bg">
    <div class="wrapper wrapper-content animated fadeInRight">
        <div class="ibox float-e-margins">
            <div class="ibox-content">
                <table class="table table-bordered table-striped text-center" data-mobile-responsive="true">
                    <thead>
                    </thead>
                    <tbody>
                    	<tr style="background-color:#fbdc89 !important;">
                            <th colSpan="7" style="font-weight: normal">
                            	<span class="title" style="font-size:17px;">项目名称：</span>
                    			<span class="title" style="font-size:15px;" id="projectName"></span>
                            </th>
                        </tr>
                        <tr style="background-color:#305cf1 !important; color:#FFFFFF;" id="toptr">
                            <td style="width:5%">序号</td>
                            <td style="width:10%">姓名</td>
                            <td style="width:12%">项目角色/担任职务</td>
							<td style="width:10%">联系方式</td>
							<td style="width:10%">所属部门</td>
							<td style="width:15%">项目职责说明</td>
                            <td style="width:15%">备注</td>
                        </tr>
						<tr style="background-color:#ececec;" id="">
							<td colspan="7" >  
								<a onclick="additem(this)">添加一条记录</a>
							</td>
						</tr>
						
                	</tbody>
          		</table>
            </div>
        </div>
    </div>
    <img src="__ROOT__/Public/Admin/img/loading.gif" class="loading" style="display:none;">	
</body>
<script src="__ROOT__/Public/Admin/js/jquery.min.js"></script>
<script src="__ROOT__/Public/Admin/js/plugins/bootstrap221/js/bootstrap.min.js"></script>
<script src="__ROOT__/Public/Admin/js/plugins/bootstrap-editable/js/bootstrap-editable.js"></script>
<script src="__ROOT__/Public/Admin/js/plugins/sweetalert/sweetalert.min.js"></script>
	
<link href="__ROOT__/Public/Admin/css/selector/selectorstyle.css" rel="stylesheet"/>
<script src="__ROOT__/Public/Admin/js/plugins/selector/selector.js"></script>
<script src="__ROOT__/Public/Admin/js/plugins/selector/jquery.ztree.all.js"></script>
<script>
	
	var i = 0;
	var projectID;
	tableGetData();
	
	function tableGetData(){
		$(".loading").show();
		$.post("<?=U('Admin/ProjectDistri/save',array('type'=>'table','id'=>$id))?>",{},function(result){
			data = result;
	 		var itmeHtml = "";
			$("#projectName").html(data.projectName);
			projectID = data.projectID;
			for(var list in data.data){
				i++;
				itmeHtml = "<tr id="+data.data[list].ID+" style=\"background-color:#FFFFFF\"><td>"+data.data[list].ID+"</td><td class='UserName"+i+"' onclick=\"tablefun('UserID"+i+"','UserName"+i+"')\" data-toggle=\"modal\" data-target=\"#myModal\">"+data.data[list].UserName+"</td><td class='UserID"+i+"' style='display:none;'><td><div class='JobDuty"+i+" myeditable'>"+data.data[list].JobDuty+"</div></td><td class='Phone"+i+"'>"+data.data[list].Phone+"</td><td class='DepName"+i+"'>"+data.data[list].DepName+"</td><td class='DepID"+i+"' style='display:none;'>"+data.data[list].DepID+"</td><td><div class='JobDutyDetail"+i+" myeditable'>"+data.data[list].JobDutyDetail+"</div></td><td><div class='Memo"+i+" myeditable'>"+data.data[list].Memo+"</div></td></tr>";
				$("#toptr").after(itmeHtml);
				TieEvent();
			}
			$(".loading").hide();
		});
	}
	
	function additem(dom){
		i++;
		var newhtml = "<tr style=\"background-color:#FFFFFF\"><td>"+($("tr").length - 2)+"</td><td class='UserName"+i+"' onclick=\"tablefun('UserID"+i+"','UserName"+i+"')\" data-toggle=\"modal\" data-target=\"#myModal\"></td><td class='UserID"+i+"' style='display:none;'></td><td><div class='JobDuty"+i+" myeditable'></div></td><td class='Phone"+i+"'></td><td class='DepName"+i+"'></td><td class='DepID"+i+"' style='display:none;'></td><td><div class='JobDutyDetail"+i+" myeditable'></div></td><td><div class='Memo"+i+" myeditable'></div></td></tr>";
	  	$(dom).parent().parent().before(newhtml);
     	$(dom).text("点击保存");
	 	$(dom).attr('onclick', '');
		$(dom).attr('onclick', "tableAddData(this)");
		TieEvent();
	}
	
	function tableAddData(dom,type){
 		var tr = $(dom).parent().parent().prev().children();
 		data = {
 			"UserID" : $(tr[2]).text(),
 			"JobDuty" : $(tr[3]).text(),
 			"JobDutyDetail" : $(tr[7]).text(),
 			"Memo" : $(tr[8]).text(),
 			"ProjectID" : projectID
 		};
 		$.post("{:U('Admin/ProjectDistri/save')}&type=add",data,function(result){
		    if (result.status) {
		    	 swal({
		            title: "继续操作？",
		            text: "操作成功！",
		            type: "success",
		            showCancelButton: false,
		            confirmButtonColor: "#DD6B55",
		            confirmButtonText: "确认",
		            cancelButtonText: "",
		            closeOnConfirm: false,
		            closeOnCancel: false
		       },function (isConfirm) {
		            document.location.reload();
		       });			
		    }else{
		    	swal("提示", result.info, "error");
		    }
		});
 	};
 	
	function tablefun(id,name){
 		openselector(id,name, function(name,id){
 			var timeid = $("."+name).parent().attr("id");
 			var timevale = $("."+id).text();
 			var reg = /\d+/g;
			var domid = reg.exec(id);
			domid = domid[0];
			var uid = $("."+id).text();
 			if(uid){
 				$.post("{:U('Admin/ProjectDistri/getUserInfo')}",{"UID":uid},function(result){
 					if(result){
 						$(".Phone"+domid).text(result.Phone);
 						$(".DepName"+domid).text(result.DepName);
 						$(".DepID"+domid).text(result.DepID);
 					}
 				});
 			}
 			if(timeid != undefined){
				var updatedata = {
					"ID" : timeid,
					"UserID" : uid
		 		}
 				tableUpdate(updatedata);
 			}
 		});
 	}
	
	function tableUpdate(updatedata){
		$(".loading").show();
 		$.post("{:U('Admin/ProjectDistri/save')}&type=update",updatedata,function(result){
 			$(".loading").hide();
 			if (result.status) {
		       swal("提示", result.info, "success");
		    }else{
		    	swal({
		            title: "",
		            text: "修改失败 "+result.info,
		            type: "error",
		            showCancelButton: false,
		            confirmButtonColor: "#DD6B55",
		            confirmButtonText: "确认",
		            cancelButtonText: "",
		            closeOnConfirm: false,
		            closeOnCancel: false
		        },function (isConfirm) {
		            document.location.reload();
		        });		
		    }
 		});
	}
	
	function TieEvent(){
		$(".JobDuty"+i).editable({
            type: "text", 
            title: "项目角色/担任职务",
            disabled: false, 
            emptytext: "",
            //mode: "inline",
			url : function(params) {  
               //console.info(params);
            },
            validate: function (value) { //字段验证
                if (!$.trim(value)) {
                    return '不能为空';
                }else{
                	var tr = $(this).parent().parent();
                	if($(tr).attr("id") == undefined) return;
                	var timeid = $(tr).attr("id");
 					var updatedata = {
		 				"JobDuty": value,
		 				"ID": timeid
		 			}
 					tableUpdate(updatedata);
                }
            }
        });
        
        $('.JobDutyDetail'+i).editable({
            type: "textarea",             
            title: "项目职责说明",           
            disabled: false,           
            emptytext: "",
            mode: "popup",          
            validate: function (value) { //字段验证
                if (!$.trim(value)) {
                    return '不能为空';
                }else{
                	var tr = $(this).parent().parent();
                	if($(tr).attr("id") == undefined) return;
                	var timeid = $(tr).attr("id");
 					var updatedata = {
		 				"JobDutyDetail": value,
		 				"ID": timeid
		 			}
 					tableUpdate(updatedata);
                }
            }
     	});
     	
     	$('.Memo'+i).editable({
            type: "textarea",             
            title: "备注",           
            disabled: false,           
            emptytext: "",
            mode: "popup",          
            validate: function (value) { //字段验证
                if (!$.trim(value)) {
                    return '不能为空';
                }else{
                	var tr = $(this).parent().parent();
                	if($(tr).attr("id") == undefined) return;
                	var timeid = $(tr).attr("id");
 					var updatedata = {
		 				"Memo": value,
		 				"ID": timeid
		 			}
 					tableUpdate(updatedata);
                }
            }
     	});
        
	}
	
</script>
<style>
	.loading{
			 width: 35px;
			 height: 35px;
			 margin: 0 auto;
			 margin-top:100px;
			 position: fixed;
			 top: 0px;
			 left: 0px;
			 right: 0px;
			 bottom: 0px;
			 margin: auto;
		}
</style>
</html>